package com.huishu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.exception.BaseException;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.goods.mapper.GiveMapper;
import com.huishu.goods.pojo.dto.GiveDTO;
import com.huishu.goods.pojo.dto.GivePageDTO;
import com.huishu.goods.pojo.model.GiveDO;
import com.huishu.goods.pojo.model.MaterialBenefitsProductGiveDO;
import com.huishu.goods.pojo.model.ProductGiveDO;
import com.huishu.goods.service.GiveService;
import com.huishu.goods.service.MaterialBenefitsProductGiveService;
import com.huishu.goods.service.ProductGiveService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-19
 */
@Service
public class GiveServiceImpl extends ServiceImpl<GiveMapper, GiveDO> implements GiveService {

    @Autowired
    private ProductGiveService productGiveService;

    @Autowired
    private MaterialBenefitsProductGiveService materialBenefitsProductGiveService;

    @Override
    public Result create(GiveDTO giveDTO) {
        GiveDO giveDO = BeanUtils.copyProperties(giveDTO, GiveDO.class);
        return this.save(giveDO) ? Result.success() : Result.fail();
    }

    @Transactional
    @Override
    public Result modify(GiveDTO giveDTO) {
        GiveDO giveDO = BeanUtils.copyProperties(giveDTO, GiveDO.class);
        // 把关联的暗号，满减，进行修改
        boolean b = this.updateById(giveDO);
        if (!b) {
            throw new BaseException("操作失败");
        }
        LambdaUpdateWrapper<MaterialBenefitsProductGiveDO> uw = Wrappers.lambdaUpdate(MaterialBenefitsProductGiveDO.class)
                .set(MaterialBenefitsProductGiveDO::getGiveProductSku, giveDO.getSkuCode())
                .eq(MaterialBenefitsProductGiveDO::getGiveProductId, giveDO.getId());
        materialBenefitsProductGiveService.update(uw);
        LambdaUpdateWrapper<ProductGiveDO> uw1 = Wrappers.lambdaUpdate(ProductGiveDO.class)
                .set(ProductGiveDO::getGiveProductSku, giveDO.getSkuCode())
                .eq(ProductGiveDO::getGiveProductId, giveDO.getId());
        productGiveService.update(uw1);
        return Result.success();
    }

    @Override
    public Result pageList(GivePageDTO givePageDTO) {
        Page<GiveDO> page = new Page<>(givePageDTO.getCurrPage(), givePageDTO.getPageSize());
        LambdaQueryWrapper<GiveDO> queryWrapper = Wrappers.lambdaQuery(GiveDO.class)
                .eq(StringUtils.isNotEmpty(givePageDTO.getSkuCode()), GiveDO::getSkuCode, givePageDTO.getSkuCode())
                .eq(StringUtils.isNotEmpty(givePageDTO.getBarCode()), GiveDO::getBarCode, givePageDTO.getBarCode())
                .like(StringUtils.isNotEmpty(givePageDTO.getName()), GiveDO::getName, givePageDTO.getName())
                .orderByDesc(GiveDO::getCreatedDate);
        return Result.success(this.baseMapper.selectPage(page, queryWrapper));
    }

    @Transactional
    @Override
    public Result delete(Long id) {
        // 查询是否关联暗号
        LambdaQueryWrapper<MaterialBenefitsProductGiveDO> queryWrapper1 = Wrappers.lambdaQuery(MaterialBenefitsProductGiveDO.class)
                .eq(MaterialBenefitsProductGiveDO::getGiveProductId, id);
        List<MaterialBenefitsProductGiveDO> list = materialBenefitsProductGiveService.list(queryWrapper1);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.fail("当前赠品关联了暗号，请解除关联后在删除");
        }
        LambdaQueryWrapper<ProductGiveDO> queryWrapper = Wrappers.lambdaQuery(ProductGiveDO.class)
                .eq(ProductGiveDO::getGiveProductId, id);
        productGiveService.remove(queryWrapper);
        this.removeById(id);
        return Result.success();
    }
}
